// index.js
import { createStore } from 'vuex';

// 从本地存储读取数据
const storedData = uni.getStorageSync('storeData');

const initialState = storedData || {
    stores: [
        {
            id: 1,
            name: '门店1',
            backgroundImage: 'https://dummyimage.com/300x200/000/fff',
            tables: [
                { id: 1, openCount: 0, totalActualAmount: 0, totalPreAmount: 0, status: '空置', records: [] },
                { id: 2, openCount: 0, totalActualAmount: 0, totalPreAmount: 0, status: '空置', records: [] }
            ]
        },
        {
            id: 2,
            name: '门店2',
            backgroundImage: 'https://dummyimage.com/300x200/000/fff',
            tables: [
                { id: 3, openCount: 0, totalActualAmount: 0, totalPreAmount: 0, status: '空置', records: [] },
                { id: 4, openCount: 0, totalActualAmount: 0, totalPreAmount: 0, status: '空置', records: [] },
                { id: 5, openCount: 0, totalActualAmount: 0, totalPreAmount: 0, status: '空置', records: [] },
                { id: 6, openCount: 0, totalActualAmount: 0, totalPreAmount: 0, status: '空置', records: [] }
            ]
        }
    ],
    currentStore: null,
    pricePerHour: 15 // 单价默认 15 元/时
};

const store = createStore({
    state: initialState,
    mutations: {
        setCurrentStore(state, store) {
            state.currentStore = store;
            saveStateToStorage(state);
        },
        startTable(state, { tableId, startTime, unitPrice }) {
            const table = state.currentStore.tables.find(table => table.id === tableId);
            if (table) {
                table.status = '使用中';
                table.openCount++;
                table.records.push({ startTime, endTime: null, actualAmount: 0, preAmount: 0, unitPrice });
            }
            saveStateToStorage(state);
        },
        endTable(state, { tableId, endTime, preAmount, actualAmount }) {
            const table = state.currentStore.tables.find(table => table.id === tableId);
            if (table) {
                table.status = '空置';
                const lastRecord = table.records[table.records.length - 1];
                if (lastRecord) {
                    lastRecord.endTime = endTime;
                    lastRecord.preAmount = preAmount;
                    lastRecord.actualAmount = actualAmount;
                    table.totalActualAmount += actualAmount;
                    table.totalPreAmount += preAmount;
                }
                // 更新门店的当日实收金额合计
                state.currentStore.dailyIncome = state.currentStore.tables.reduce((total, table) => total + table.totalActualAmount, 0);
            }
            saveStateToStorage(state);
        },
        adjustPrice(state, newPrice) {
            state.pricePerHour = newPrice;
            saveStateToStorage(state);
        }
    },
    getters: {
        monthlyIncome: (state) => (storeId) => {
            const store = state.stores.find(store => store.id === storeId);
            if (!store) return 0;
            // 这里简单模拟按月统计，实际中需要根据日期筛选记录
            return store.tables.reduce((total, table) => total + table.totalActualAmount, 0);
        },
        dailyIncome: (state) => (storeId) => {
            const store = state.stores.find(store => store.id === storeId);
            if (!store) return 0;
            // 这里简单模拟按天统计，实际中需要根据日期筛选记录
            return store.tables.reduce((total, table) => total + table.totalActualAmount, 0);
        }
    }
});

// 保存 state 到本地存储
function saveStateToStorage(state) {
    uni.setStorageSync('storeData', state);
}

export default store;